package com.dhcc.core.framework.referer;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.dhcc.core.components.httpClient.model.exception.HttpRequestException;
import com.dhcc.core.framework.util.CommonUtil;

/**
 * Referer过滤器
 * 
 * @ClassName: RefererFilter
 * @Description: 描述
 * @author: cyf
 * @date: 2018年1月2日 上午9:39:55
 */
public class RefererFilter implements Filter {

    FilterConfig filterConfig = null;

    private List<String> hostExclusion = null;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void destroy() {
        this.filterConfig = null;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // 如果是空的就不检测了
        if (CommonUtil.isNotEmpty(hostExclusion)) {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            String referer = httpServletRequest.getHeader("referer");
            if (CommonUtil.isNotEmpty(referer)) {
                // 这个要加上
                if(!CommonUtil.contains(hostExclusion, httpServletRequest.getServerName())){
                    hostExclusion.add(httpServletRequest.getServerName());
                }
                boolean isBadReferer = true;
                for (int i = 0; i < hostExclusion.size(); i++) {
                    if (CommonUtil.isNotEmpty(hostExclusion.get(i))) {
                        if (referer.matches("^(http|https)+://(" + hostExclusion.get(i) + ")(.*)")) {
                            isBadReferer = false;
                            break;
                        }
                    }
                }
                if (isBadReferer) {
                    // 多个服务器网址无法匹配的时候，即表示此为错误的referer
                    throw new HttpRequestException();
                }
            } else {
                // referer 为空时只允许这几个地址
                String servletPath = httpServletRequest.getServletPath();
                if (CommonUtil.isNotEmpty(servletPath)) {
                    // 如果不是这几个地址就不让访问了
                    if (!(CommonUtil.equals(servletPath, "") || CommonUtil.equals(servletPath, "/")
                            || CommonUtil.equals(servletPath, "/login") || CommonUtil.equals(servletPath, "/logout"))) {
                        throw new HttpRequestException();
                    }
                }
            }
        }
        chain.doFilter(request, response);
    }

    public List<String> getHostExclusion() {
        return hostExclusion;
    }

    public void setHostExclusion(List<String> hostExclusion) {
        this.hostExclusion = hostExclusion;
    }
}